package com.rc.saas.tenant.controller.help;

import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.utils.BeanUtil;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.help.UserGuide;
import com.rc.saas.tenant.model.help.UserGuideExample;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.service.help.UserGuideService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.vo.help.UserGuideResultVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author frog
 * @date 2021/3/9
 */
@Controller
@RequestMapping("/userGuide")
public class UserGuideController {

    private static Logger logger = LogManager.getLogger(UserGuideController.class);

    @Autowired
    private UserGuideService userGuideService;


    /**
     * 跳转用户指南(文章)页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        UserGuideExample example = new UserGuideExample();
        example.createCriteria().andGuideTypeEqualTo("0");
        List<UserGuide> userGuides = userGuideService.selectByExample(example);
        model.addAttribute("userGuides", userGuides);
        return "/templates/guide/userGuide";
    }

    /**
     * 返回目录列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listUserGuide", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listUserGuide(BaseSearch baseSearch, Long userGuideId) {
        PageInfo<UserGuideResultVo> pageInfo = userGuideService.listUserGuide(baseSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 跳转增加目录页面
     *
     * @return
     */
    @RequestMapping(value = "/addCatalog", method = RequestMethod.GET)
    public String addCatalog() {
        return "/templates/guide/addCatalog";
    }


    /**
     * 跳转增加文章页面
     *
     * @return
     */
    @RequestMapping(value = "/addArticle", method = RequestMethod.GET)
    public String addArticle(Model model) {
        TenantUser user = ShiroUtils.getSessionUser();
        UserGuideExample example = new UserGuideExample();
        example.createCriteria().andTenantCodeEqualTo(user.getTenantCode()).andGuideTypeEqualTo("0");
        List<UserGuide> userGuides = userGuideService.selectByExample(example);
        model.addAttribute("userGuides", userGuides);
        return "/templates/guide/addArticle";
    }

    /**
     * 跳转编辑文章页面
     *
     * @return
     */
    @RequestMapping(value = "/editArticle/{userGuideId}", method = RequestMethod.GET)
    public String editArticle(@PathVariable("userGuideId") Long userGuideId, Model model) {
        UserGuide userGuide = userGuideService.selectByPrimaryKey(userGuideId);
        model.addAttribute("userGuide", userGuide);
        return "/templates/guide/editArticle";
    }


    /**
     * 保存/编辑用户指南
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateUserGuide", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveOrUpdateBanner(UserGuide userGuide) {

        try {
            if ("1".equals(userGuide.getGuideType())) {
                if (!BeanUtil.isValid(userGuide.getParentId(), userGuide.getLink(), userGuide.getContent())) {
                    return TResponse.FAIL("请输入正确的参数");
                }
            } else {
                if (!BeanUtil.isValid(userGuide.getContent())) {
                    return TResponse.FAIL("请输入正确的参数");
                }
            }

            if (userGuide.getUserGuideId() != null) {
                userGuideService.updateByPrimaryKeySelective(userGuide);
            } else {
                TenantUser user = ShiroUtils.getSessionUser();
                userGuide.setTenantCode(user.getTenantCode());
                userGuide.setSort(0);
                userGuideService.insertSelective(userGuide);
            }
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("保存用户指南失败", e);
            return TResponse.FAIL("操作失败");
        }
    }

    /**
     * 删除目录
     *
     * @return
     */
    @RequestMapping(value = "/deleteArticle", method = RequestMethod.POST)
    @ResponseBody
    public TResponse deleteArticle(Long userGuideId) {
        try {
            userGuideService.deleteByPrimaryKey(userGuideId);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("删除文章失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 跳转用户指南(目录)页面
     *
     * @return
     */
    @RequestMapping(value = "/catalogManagement", method = RequestMethod.GET)
    public String catalogManagement() {
        return "/templates/guide/catalogManagement";
    }

    /**
     * 返回目录列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listCatalogManagement", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listCatalogManagement(BaseSearch baseSearch) {
        PageInfo<UserGuide> pageInfo = userGuideService.listCatalogManagement(baseSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }
}
